package com.example.arithmeticleetcode.leetcode.jan;


import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-05 09:09
 **/
public class Demo03 {

    public static void main(String[] args) {
        int[] deleciousness = {1, 1};
        new Demo03().countPairs(deleciousness);
    }


    public int countPairs2(int[] deliciousness) {
        int ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : deliciousness) {
            for (int j = 0; j < 22; j++) {
                int powerOfTwo = (int)Math.pow(2, j);
                int diff = powerOfTwo - i;
                if (diff >= 0 && map.containsKey(diff)) {
                    ans += map.get(diff);
                }
            }
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        ans %= (1e9 + 7);
        return ans;
    }


    public int countPairs(int[] deliciousness) {
        Set<Integer> set = new HashSet<>();
        set.add(0);
        set.add(1);
        set.add(2);
        double temp = 2;
        for (int i = 2; i <= 20; i++) {
            temp *= 2;
            if (temp > Integer.MAX_VALUE) {
                set.add((int)(temp % (Math.pow(10, 9) + 7)));
            } else {
                set.add((int)temp);
            }
        }
        int count = 0;
        for (int i = 0; i < deliciousness.length; i++) {
            for (int j = i + 1; j < deliciousness.length; j++) {
                long tempSum = deliciousness[i] + deliciousness[j];
                if (tempSum > Integer.MAX_VALUE) {
                    tempSum = (int)(tempSum % (Math.pow(10, 9) + 7));
                }
                if (set.contains((int)tempSum)) count++;
            }
        }
        return count;
    }

    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> returnList = new ArrayList<>();
        if (s == null || s.length() < 3) return returnList;
        int startIndex = 0;
        char[] chars = s.toCharArray();
        List<Integer> list = null;
        for (int i = 1; i < chars.length; i++) {
            //相等
            if (chars[startIndex] == chars[i]) continue;
            //不相等
            if (i - startIndex >= 3) {
                list = new ArrayList<>();
                list.add(startIndex);
                list.add(i - 1);
                returnList.add(list);
            }
            startIndex = i;
        }
        if (chars.length - startIndex > 3) {
            list = new ArrayList<>();
            list.add(startIndex);
            list.add(chars.length - 1);
            returnList.add(list);
        }
        return returnList;
    }
}
